.\"
.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
.\" permission to reproduce portions of its copyrighted documentation.
.\" Original documentation from The Open Group can be obtained online at
.\" http://www.opengroup.org/bookstore/.
.\"
.\" The Institute of Electrical and Electronics Engineers and The Open
.\" Group, have given us permission to reprint portions of their
.\" documentation.
.\"
.\" In the following statement, the phrase ``this text'' refers to portions
.\" of the system documentation.
.\"
.\" Portions of this text are reprinted and reproduced in electronic form
.\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
.\" Standard for Information Technology -- Portable Operating System
.\" Interface (POSIX), The Open Group Base Specifications Issue 6,
.\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
.\" Engineers, Inc and The Open Group.  In the event of any discrepancy
.\" between these versions and the original IEEE and The Open Group
.\" Standard, the original IEEE and The Open Group Standard is the referee
.\" document.  The original Standard can be obtained online at
.\" http://www.opengroup.org/unix/online.html.
.\"
.\" This notice shall appear on any product containing this material.
.\"
.\" The contents of this file are subject to the terms of the
.\" Common Development and Distribution License (the "License").
.\" You may not use this file except in compliance with the License.
.\"
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
.\" or http://www.opensolaris.org/os/licensing.
.\" See the License for the specific language governing permissions
.\" and limitations under the License.
.\"
.\" When distributing Covered Code, include this CDDL HEADER in each
.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
.\" If applicable, add the following below this CDDL HEADER, with the
.\" fields enclosed by brackets "[]" replaced with your own identifying
.\" information: Portions Copyright [yyyy] [name of copyright owner]
.\"
.\"
.\" Copyright 1989 AT&T
.\" Portions Copyright (c) 1992, X/Open Company Limited.  All Rights Reserved.
.\" Portions Copyright (c) 1994 Man-cgi 1.15, Panagiotis Christias (christia@softlab.ntua.gr)
.\" Portions Copyright (c) 1996-2008 Modified for NetBSD by Kimmo Suominen (kimmo@suominen.com)
.\" Copyright (c) 2008, Sun Microsystems, Inc.  All Rights Reserved.
.\" Copyright 2014 Garrett D'Amore <garrett@damore.org>
.\" Copyright (c) 2014, Joyent, Inc.
.\"
.TH STRING 3C "Mar 23, 2016"
.SH NAME
string, strcasecmp, strcasecmp_l, strncasecmp, strncasecmp_l, strcat, strncat,
strlcat, strchr, strchrnul, strrchr, strcmp, strncmp, stpcpy, stpncpy, strcpy,
strncpy, strlcpy, strcspn, strspn, strdup, strndup, strdupa, strndupa, strlen,
strnlen, strpbrk, strsep, strstr, strnstr, strcasestr, strtok, strtok_r \-
string operations
.SH SYNOPSIS
.LP
.nf
#include <strings.h>

\fBint\fR \fBstrcasecmp\fR(\fBconst char *\fR\fIs1\fR, \fBconst char *\fR\fIs2\fR);
.fi
.LP
.nf
\fBint\fR \fBstrcasecmp_l\fR(\fBconst char *\fR\fIs1\fR, \fBconst char *\fR\fIs2\fR, \fBlocale_t\fR \fIloc\fR);
.fi
.LP
.nf
\fBint\fR \fBstrncasecmp\fR(\fBconst char *\fR\fIs1\fR, \fBconst char *\fR\fIs2\fR, \fBsize_t\fR \fIn\fR);
.fi
.LP
.nf
\fBint\fR \fBstrncasecmp_l\fR(\fBconst char *\fR\fIs1\fR, \fBconst char *\fR\fIs2\fR, \fBsize_t\fR \fIn\fR, \fBlocale_t\fR \fIloc\fR);
.fi
.LP
.nf
#include <string.h>

\fBchar *\fR\fBstrcat\fR(\fBchar *restrict\fR \fIs1\fR, \fBconst char *restrict\fR \fIs2\fR);
.fi
.LP
.nf
\fBchar *\fR\fBstrncat\fR(\fBchar *restrict\fR \fIs1\fR, \fBconst char *restrict\fR \fIs2\fR, \fBsize_t\fR \fIn\fR);
.fi
.LP
.nf
\fBsize_t\fR \fBstrlcat\fR(\fBchar *\fR\fIdst\fR, \fBconst char *\fR\fIsrc\fR, \fBsize_t\fR \fIdstsize\fR);
.fi
.LP
.nf
\fBchar *\fR\fBstrchr\fR(\fBconst char *\fR\fIs\fR, \fBint\fR \fIc\fR);
.fi
.LP
.nf
\fBchar *\fR\fBstrrchr\fR(\fBconst char *\fR\fIs\fR, \fBint\fR \fIc\fR);
.fi
.LP
.nf
\fBint\fR \fBstrcmp\fR(\fBconst char *\fR\fIs1\fR, \fBconst char *\fR\fIs2\fR);
.fi
.LP
.nf
\fBint\fR \fBstrncmp\fR(\fBconst char *\fR\fIs1\fR, \fBconst char *\fR\fIs2\fR, \fBsize_t\fR \fIn\fR);
.fi
.LP
.nf
\fBchar *\fR\fBstpcpy\fR(\fBchar *restrict\fR \fIs1\fR, \fBconst char *restrict\fR \fIs2\fR);
.fi
.LP
.nf
\fBchar *\fR\fBstpncpy\fR(\fBchar *restrict\fR \fIs1\fR, \fBconst char *restrict\fR \fIs2\fR, \fBsize_t\fR \fIn\fR);
.fi
.LP
.nf
\fBchar *\fR\fBstrcpy\fR(\fBchar *restrict\fR \fIs1\fR, \fBconst char *restrict\fR \fIs2\fR);
.fi
.LP
.nf
\fBchar *\fR\fBstrncpy\fR(\fBchar *restrict\fR \fIs1\fR, \fBconst char *restrict\fR \fIs2\fR, \fBsize_t\fR \fIn\fR);
.fi
.LP
.nf
\fBsize_t\fR \fBstrlcpy\fR(\fBchar *\fR\fIdst\fR, \fBconst char *\fR\fIsrc\fR, \fBsize_t\fR \fIdstsize\fR);
.fi
.LP
.nf
\fBsize_t\fR \fBstrcspn\fR(\fBconst char *\fR\fIs1\fR, \fBconst char *\fR\fIs2\fR);
.fi
.LP
.nf
\fBsize_t\fR \fBstrspn\fR(\fBconst char *\fR\fIs1\fR, \fBconst char *\fR\fIs2\fR);
.fi
.LP
.nf
\fBchar *\fR\fBstrdup\fR(\fBconst char *\fR\fIs1\fR);
.fi
.LP
.nf
\fBchar *\fR\fBstrndup\fR(\fBconst char *\fR\fIs1\fR, \fBsize_t\fR \fIn\fR);
.fi
.LP
.nf
\fBchar *\fR\fBstrdupa\fR(\fBconst char *\fR\fIs1\fR);
.fi
.LP
.nf
\fBchar *\fR\fBstrndupa\fR(\fBconst char *\fR\fIs1\fR, \fBsize_t\fR \fIn\fR);
.fi
.LP
.nf
\fBsize_t\fR \fBstrlen\fR(\fBconst char *\fR\fIs\fR);
.fi
.LP
.nf
\fBsize_t\fR \fBstrnlen\fR(\fBconst char *\fR\fIs\fR, \fBsize_t\fR \fIn\fR);
.fi
.LP
.nf
\fBchar *\fR\fBstrpbrk\fR(\fBconst char *\fR\fIs1\fR, \fBconst char *\fR\fIs2\fR);
.fi
.LP
.nf
\fBchar *\fR\fBstrsep\fR(\fBchar **\fR\fIstringp\fR, \fBconst char *\fR\fIdelim\fR);
.fi
.LP
.nf
\fBchar *\fR\fBstrstr\fR(\fBconst char *\fR\fIs1\fR, \fBconst char *\fR\fIs2\fR);
.fi
.LP
.nf
\fBchar *\fR\fBstrnstr\fR(\fBconst char *\fR\fIs1\fR, \fBconst char *\fR\fIs2\fR, \fBsize_t\fR \fIn\fR);
.fi
.LP
.nf
\fBchar *\fR\fBstrcasestr\fR(\fBconst char *\fR\fIs1\fR, \fBconst char *\fR\fIs2\fR);
.fi
.LP
.nf
\fBchar *\fR\fBstrcasestr_l\fR(\fBconst char *\fR\fIs1\fR, \fBconst char *\fR\fIs2\fR, \fBlocale_t\fR \fIloc\fR);
.fi
.LP
.nf
\fBchar *\fR\fBstrtok\fR(\fBchar *restrict\fR \fIs1\fR, \fBconst char *restrict\fR \fIs2\fR);
.fi
.LP
.nf
\fBchar *\fR\fBstrtok_r\fR(\fBchar *restrict\fR \fIs1\fR, \fBconst char *restrict\fR \fIs2\fR,
     \fBchar **restrict\fR \fIlasts\fR);
.fi
.SS "ISO C++"
.LP
.nf
#include <string.h>

\fBconst char *\fR\fBstrchr\fR(\fBconst char *\fR\fIs\fR, \fBint\fR \fIc\fR);
.fi
.LP
.nf
\fBconst char *\fR\fBstrchrnul\fR(\fBconst char *\fR\fIs\fR, \fBint\fR \fIc\fR);
.fi
.LP
.nf
\fBconst char *\fR\fBstrpbrk\fR(\fBconst char *\fR\fIs1\fR, \fBconst char *\fR\fIs2\fR);
.fi
.LP
.nf
\fBconst char *\fR\fBstrrchr\fR(\fBconst char *\fR\fIs\fR, \fBint\fR \fIc\fR);
.fi
.LP
.nf
\fBconst char *\fR\fBstrstr\fR(\fBconst char *\fR\fIs1\fR, \fBconst char *\fR\fIs2\fR);
.fi
.LP
.nf
#include <cstring>

\fBchar *std::\fR\fBstrchr\fR(\fBchar *\fR\fIs\fR, \fBint\fR \fIc\fR);
.fi
.LP
.nf
\fBchar *std::\fR\fBstrpbrk\fR(\fBchar *\fR\fIs1\fR, \fBconst char *\fR\fIs2\fR);
.fi
.LP
.nf
\fBchar *std::\fR\fBstrrchr\fR(\fBchar *\fR\fIs\fR, \fBint\fR \fIc\fR);
.fi
.LP
.nf
\fBchar *std::\fR\fBstrstr\fR(\fBchar *\fR\fIs1\fR, \fBconst char *\fR\fIs2\fR);
.fi
.SH DESCRIPTION
.LP
The arguments \fIs\fR, \fIs1\fR, and \fIs2\fR point to strings (arrays of
characters terminated by a null character). The \fBstrcat()\fR, \fBstrncat()\fR,
\fBstrlcat()\fR, \fBstrcpy()\fR, \fBstpcpy()\fR, \fBstpncpy()\fR,
\fBstrncpy()\fR, \fBstrlcpy()\fR, \fBstrsep()\fR, \fBstrtok()\fR, and
\fBstrtok_r()\fR functions all alter their first argument. Additionally, the
\fBstrcat()\fR, \fBstpcpy()\fR, and \fBstrcpy()\fR functions do not check for
overflow of the array.
.SS "\fBstrcasecmp()\fR, \fBstrncasecmp()\fR"
.LP
The \fBstrcasecmp()\fR and \fBstrncasecmp()\fR functions are case-insensitive
versions of  \fBstrcmp()\fR and \fBstrncmp()\fR respectively, described below.
.LP
The \fBstrcasecmp()\fR and \fBstrncasecmp()\fR functions compare two strings
byte-by-byte, after
converting each upper-case character to lower-case (as determined by the
\fBLC_CTYPE\fR category of the current locale).  Note that neither the contents
pointed to by \fIs1\fR nor \fIs2\fR are modified.
.LP
The functions return an integer
greater than, equal to, or less than 0, if the string pointed to by \fIs1\fR
is greater than, equal to, or less than the string pointed to by \fIs2\fR
respectively. The sign of a non-zero return value is determined  by the sign of
the difference between the values of the first pair of bytes that differ in the
.LP
The \fBstrncasecmp()\fR function examines at most \fIn\fR bytes from each
string.
.SS "\fBstrcasecmp_l()\fR, \fBstrncasecmp_l()\fR"
.LP
The \fBstrcasecmp_l()\fR and \fBstrncasecmp_l()\fR functions behave identically
to \fBstrcasecmp()\fR and \fBstrncasecmp()\fR, except instead of operating in
the current locale, they instead operate in the locale specified by \fIloc\fR.
.SS "\fBstrcat()\fR, \fBstrncat()\fR, \fBstrlcat()\fR"
.LP
The \fBstrcat()\fR function appends a copy of string \fIs2\fR, including the
terminating null character, to the end of string \fIs1\fR. The \fBstrncat()\fR
function appends at most \fIn\fR characters of \fIs2\fR to \fIs1\fR, not
including any terminating null character, and then appends a null character.
Each returns a pointer to the null-terminated result. The initial character of
\fIs2\fR overrides the null character at the end of \fIs1\fR. If copying takes
place between objects that overlap, the behavior of \fBstrcat()\fR,
\fBstrncat()\fR, and \fBstrlcat()\fR is undefined.
.LP
The \fBstrlcat()\fR function appends  at most
(\fIdstsize\fR-\fBstrlen\fR(\fIdst\fR)-1) characters of \fIsrc\fR to \fIdst\fR
(\fIdstsize\fR being the  size of the  string buffer \fIdst\fR). If the string
pointed to by \fIdst\fR contains a null-terminated string that fits into
\fIdstsize\fR bytes when \fBstrlcat()\fR is called, the string pointed to by
\fIdst\fR will be a null-terminated string that fits in \fIdstsize\fR bytes
(including the terminating null character) when it completes, and the initial
character of \fIsrc\fR will override the null character at  the end of
\fIdst\fR. If the string pointed to by \fIdst\fR is longer than \fIdstsize\fR
bytes when \fBstrlcat()\fR is called, the string pointed to by \fIdst\fR will
not be changed. The function returns
\fBmin\fR{\fIdstsize\fR,\fBstrlen\fR(\fIdst\fR)}+\fBstrlen\fR(\fIsrc\fR).
Buffer overflow can be checked as  follows:
.sp
.in +2
.nf
if (strlcat(dst, src, dstsize) >= dstsize)
        return \(mi1;
.fi
.in -2
.SS "\fBstrchr()\fR, \fBstrrchr()\fR, \fBstrchrnul()\fR"
.LP
The \fBstrchr()\fR function returns a pointer to the first occurrence of
\fIc\fR (converted to a  \fBchar\fR) in string \fIs\fR, or a null pointer if
\fIc\fR does not occur in the string. The \fBstrrchr()\fR function returns a
pointer to the last occurrence of \fIc\fR. The null character terminating a
string is considered to be part of the string. The \fBstrchrnul()\fR function
behaves similarly to \fBstrchr()\fR, except when the character \fBc\fR is not
found, it returns a pointer to the null terminator of the string \fBs\fR and not
a null pointer.
.SS "\fBstrcmp()\fR, \fBstrncmp()\fR"
.LP
The \fBstrcmp()\fR function compares two strings byte-by-byte, according to the
ordering of your machine's character set.  The function returns an integer
greater than, equal to, or less than 0, if  the string pointed to by \fIs1\fR
is greater than, equal to, or less than the string pointed to by \fIs2\fR
respectively. The sign of a non-zero return value is determined  by the sign of
the difference between the values of the first pair of bytes that differ in the
strings being compared. The \fBstrncmp()\fR function makes the same comparison
but looks at a maximum of \fIn\fR bytes. Bytes following a null byte are not
compared.
.SS "\fBstrcpy()\fR, \fBstrncpy()\fR, \fBstrlcpy()\fR"
.LP
The \fBstrcpy()\fR function copies string \fIs2\fR to \fIs1\fR, including the
terminating null character, stopping after the null character has been copied.
The \fBstrncpy()\fR function copies exactly \fIn\fR bytes, truncating \fIs2\fR
or adding null characters to \fIs1\fR if necessary. The result will not be
null-terminated if the length of \fIs2\fR is \fIn\fR or more. Both the
\fBstrcpy()\fR and \fBstrncpy()\fR functions return \fIs1\fR. If copying takes
place between objects that overlap, the behavior of \fBstrcpy()\fR,
\fBstrncpy()\fR, and \fBstrlcpy()\fR is undefined.
.LP
The \fBstrlcpy()\fR function copies  at most \fIdstsize\fR\(mi1 characters
(\fIdstsize\fR being the  size of the  string buffer \fIdst\fR) from \fIsrc\fR
to \fIdst\fR,  truncating \fIsrc\fR if necessary.  The  result is always
null-terminated. The function returns \fBstrlen\fR(\fIsrc\fR). Buffer overflow
can be checked as  follows:
.sp
.in +2
.nf
if (strlcpy(dst, src, dstsize) >= dstsize)
        return \(mi1;
.fi
.in -2
.SS "\fBstpcpy()\fR, \fBstpncpy()\fR"
.LP
The \fBstpcpy()\fR and \fBstpncpy()\fR functions behave identically to
\fBstrcpy()\fR and \fBstrncpy()\fR respectively; however, instead of returning a
pointer to the beginning of \fIs1\fR, they return a pointer to the terminating
null character.
.SS "\fBstrcspn()\fR, \fBstrspn()\fR"
.LP
The \fBstrcspn()\fR function returns the length of the initial segment of
string \fIs1\fR that consists entirely of characters not from string \fIs2\fR.
The \fBstrspn()\fR function returns the length of the initial segment of string
\fIs1\fR that consists entirely of characters from string \fIs2\fR.
.SS "\fBstrdup()\fR, \fBstrndup()\fR, \fBstrdupa()\fR, \fBstrndupa()\fR"
.LP
The \fBstrdup()\fR function returns a pointer to a new string that is a
duplicate of the string pointed to by  \fIs1\fR. The returned pointer can be
passed to \fBfree()\fR. The space for the new string is obtained using
\fBmalloc\fR(3C). If the new string cannot be created, a null pointer is
returned and \fBerrno\fR may be set to \fBENOMEM\fR to indicate that the
storage space available is insufficient. The \fBstrndup()\fR function is
identical to \fBstrdup()\fR, except it copies at most \fIn\fR bytes from
\fBs1\fR and ensures the copied string is always null terminated.
.LP
The functions \fBstrdupa()\fR and \fBstrndupa()\fR behave identically to
\fBstrdup()\fR and \fBstrndup()\fR respectively; however, instead of allocating
memory using \fBmalloc\fR(3C), they use \fBalloca\fR(3C). These functions are
provided for compatibility only, their use is strongly discouraged due to their
use of \fBalloca\fR(3C).
.SS "\fBstrlen()\fR, \fBstrnlen()\fR"
The \fBstrlen()\fR function returns the number of bytes in \fIs\fR, not
including the terminating null character.
.LP
The \fBstrnlen()\fR function returns the smaller of \fIn\fR or the number of
bytes in \fIs\fR, not including the terminating null character. The
\fBstrnlen()\fR function never examines more than \fIn\fR bytes of the string
pointed to by \fIs\fR.
.SS "\fBstrpbrk()\fR"
.LP
The \fBstrpbrk()\fR function returns a pointer to the first occurrence in
string \fIs1\fR of any character from string \fIs2\fR, or a null pointer if no
character from \fIs2\fR exists in \fIs1\fR.
.SS "\fBstrsep()\fR"
.LP
The \fBstrsep()\fR function locates, in the null-terminated string referenced
by *\fIstringp\fR, the first occurrence of any character in the string
\fIdelim\fR (or the terminating `\e0' character) and replaces it with a `\e0'.
The location of the next character after the delimiter character (or
\fINULL\fR, if the end of the string was reached) is stored in *\fIstringp\fR.
The original value of *\fIstringp\fR is returned.
.LP
An ``empty'' field (one caused by two adjacent delimiter characters) can be
detected by comparing the location referenced by the pointer returned by
\fBstrsep()\fR to `\e0'.
.LP
If *\fIstringp\fR is initially \fINULL\fR, \fBstrsep()\fR returns \fINULL\fR.
.SS "\fBstrstr()\fR, \fBstrnstr()\fR, \fBstrcasestr()\fR, \fBstrcasestr_l()\fR"
.LP
The \fBstrstr()\fR function locates the first occurrence of the string \fIs2\fR
(excluding the terminating null character) in string \fIs1\fR and returns a
pointer to the located string, or a null pointer if the string is not found. If
\fIs2\fR points to a string with zero length (that is, the string \fB""\fR),
the function returns  \fIs1\fR. The \fBstrnstr()\fR function performs the same
search as \fBstrstr()\fR, but only considers up to \fIn\fR bytes of \fIs1\fR.
Bytes following a null byte are not compared.
.sp
.LP
The \fBstrcasestr()\fR and \fBstrcasestr_l()\fR functions are similar to
\fBstrstr()\fR, but both functions ignore the case of both \fBs1\fR and
\fBs2\fR. Where as the \fBstrcasestr()\fR function operates in the current
locale, the \fBstrcasestr_l()\fR function operates in the locale specified by
\fIloc\fR.
.SS "\fBstrtok()\fR"
.LP
A sequence of calls to \fBstrtok()\fR breaks the string pointed to by \fIs1\fR
into a sequence of tokens, each of which is delimited by a byte from the string
pointed to by \fIs2\fR. The first call in the sequence has \fIs1\fR as its
first argument, and is followed by calls with a null pointer as their first
argument. The separator string pointed to by \fIs2\fR can be different from
call to call.
.LP
The first call in the sequence searches the string pointed to by \fIs1\fR for
the first byte that is not contained in the current separator string pointed to
by \fIs2\fR. If no such byte is found, then there are no tokens in the string
pointed to by \fIs1\fR and \fBstrtok()\fR returns a null pointer. If such a
byte is found, it is the start of the first token.
.LP
The \fBstrtok()\fR function then searches from there for a byte that is
contained in the current separator string. If no such byte is found, the
current token extends to the end of the string pointed to by \fIs1\fR, and
subsequent searches for a token return a null pointer. If such a byte is found,
it is overwritten by a null byte that terminates the current token. The
\fBstrtok()\fR function saves a pointer to the following byte in
thread-specific data, from which the next search for a token starts.
.LP
Each subsequent call, with a null pointer as the value of the first argument,
starts searching from the saved pointer and behaves as described above.
.LP
See Example 1, 2, and 3 in the \fBEXAMPLES\fR section for examples of
\fBstrtok()\fR usage and the explanation in \fBNOTES\fR.
.SS "\fBstrtok_r()\fR"
.LP
The \fBstrtok_r()\fR function considers the null-terminated string \fIs1\fR as
a sequence of zero or more text tokens separated by spans of one or more
characters from the separator string \fIs2\fR. The argument \fIlasts\fR points
to a user-provided pointer which points to stored information necessary for
\fBstrtok_r()\fR to continue scanning the same string.
.LP
In the first call to \fBstrtok_r()\fR, \fIs1\fR points to a null-terminated
string, \fIs2\fR to a null-terminated string of separator characters, and the
value pointed to by \fIlasts\fR is ignored. The \fBstrtok_r()\fR function
returns a pointer to the first character of the first token, writes a null
character into \fIs1\fR immediately following the returned token, and updates
the pointer to which \fIlasts\fR points.
.LP
In subsequent calls, \fIs1\fR is a null pointer and \fIlasts\fR is unchanged
from the previous call so that subsequent calls move through the string
\fIs1\fR, returning successive tokens until no tokens remain. The separator
string \fIs2\fR can be different from call to call. When no token remains in
\fIs1\fR, a null pointer is returned.
.LP
See Example 3 in the \fBEXAMPLES\fR section for an example of \fBstrtok_r()\fR
usage and the explanation in \fBNOTES\fR.
.SH EXAMPLES
.LP
\fBExample 1 \fRSearch for word separators.
.LP
The following example searches for tokens separated by space characters.

.sp
.in +2
.nf
#include <string.h>
\&...
char *token;
char line[] = "LINE TO BE SEPARATED";
char *search = " ";

/* Token will point to "LINE". */
token = strtok(line, search);

/* Token will point to "TO". */
token = strtok(NULL, search);
.fi
.in -2

.LP
\fBExample 2 \fRBreak a Line.
.LP
The following example uses strtok to break a line into two character strings
separated by any combination of SPACEs, TABs, or NEWLINEs.

.sp
.in +2
.nf
#include <string.h>
\&...
struct element {
       char *key;
       char *data;
};
\&...
char line[LINE_MAX];
char *key, *data;
\&...
key = strtok(line, " \en");
data = strtok(NULL, " \en");
.fi
.in -2

.LP
\fBExample 3 \fRSearch for tokens.
.LP
The following example uses both \fBstrtok()\fR and \fBstrtok_r()\fR to search
for tokens separated by one or more characters from the string pointed to by
the second argument, "/".

.sp
.in +2
.nf
#define __EXTENSIONS__
#include <stdio.h>
#include <string.h>

int
main() {
        char *buf="5/90/45";
        char *token;
        char *lasts;

        printf("tokenizing \e"%s\e" with strtok():\en", buf);
        if ((token = strtok(buf, "/")) != NULL) {
                printf("token = "%s\e"\en", token);
                while ((token = strtok(NULL, "/")) != NULL) {
                        printf("token = \e"%s\e"\en", token);
                }
        }

        buf = "//5//90//45//";
        printf("\entokenizing \e"%s\e" with strtok_r():\en", buf);
        if ((token = strtok_r(buf, "/", &lasts)) != NULL) {
                printf("token = \e"%s\e"\en", token);
                while ((token = strtok_r(NULL, "/", &lasts)) != NULL) {
                        printf("token = \e"%s\e"\en", token);
                }
        }
}
.fi
.in -2

.LP
When compiled and run, this example produces the following output:

.sp
.in +2
.nf
tokenizing "5/90/45" with \fBstrtok()\fR:
token = "5"
token = "90"
token = "45"

tokenizing "//5//90//45//" with \fBstrtok_r()\fR:
token = "5"
token = "90"
token = "45"
.fi
.in -2

.SH ATTRIBUTES
.LP
See \fBattributes\fR(7) for descriptions of the following attributes:
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE	ATTRIBUTE VALUE
_
Interface Stability	See below.
_
MT-Level	See below.
_
Standard	See below.
.TE

.LP
The
\fBstrlcat()\fR, \fBstrlcpy()\fR, and \fBstrsep()\fR functions are Committed.
All the rest are Standard.
.LP
The \fBstrtok()\fR, \fBstrdup()\fR, and \fBstrndup()\fR functions are MT-Safe.
The remaining functions are Async-Signal-Safe.
.LP
For all except \fBstrlcat()\fR, \fBstrlcpy()\fR, and \fBstrsep()\fR, see
\fBstandards\fR(7).
.SH SEE ALSO
.LP
.BR malloc (3C),
.BR newlocale (3C),
.BR setlocale (3C),
.BR strxfrm (3C),
.BR uselocale (3C),
.BR attributes (7),
.BR standards (7)
.SH NOTES
.LP
When compiling multithreaded applications, the \fB_REENTRANT\fR flag must be
defined on the compile line. This flag should only be used in multithreaded
applications.
.LP
A single-threaded application can gain access to \fBstrtok_r()\fR only by
defining \fB__EXTENSIONS__\fR or by defining \fB_POSIX_C_SOURCE\fR to a value
greater than or equal to 199506L.
.LP
Except where noted otherwise, all of these functions assume the default
locale ``C.'' For some locales,
\fBstrxfrm\fR(3C) should be applied to the strings before they are passed to
the functions.
.LP
The \fBstrtok()\fR function is safe to use in multithreaded applications
because it saves its internal state in a thread-specific data area.  However,
its use is discouraged, even for single-threaded applications. The
\fBstrtok_r()\fR function should be used instead.
.LP
Do not pass the address of a character string literal as the argument \fIs1\fR
to either \fBstrtok()\fR or \fBstrtok_r()\fR. Similarly, do not pass a pointer
to the address of a character string literal as the argument \fIstringp\fR to
\fBstrsep()\fR. These functions can modify the storage pointed to by \fIs1\fR
in the case of \fBstrtok()\fR and \fBstrtok_r()\fR or *\fIstringp\fR in the
case of \fBstrsep()\fR. The C99 standard specifies that attempting to modify
the storage occupied by a string literal results in undefined behavior. This
allows compilers (including \fBgcc\fR and the Sun Studio compilers when the
\fB-xstrconst\fR flag is used) to place string literals in read-only memory.
Note that in Example 1 above, this problem is avoided because the variable
\fIline\fR is declared as a writable array of type \fBchar\fR that is
initialized by a string literal rather than a pointer to \fBchar\fR that points
to a string literal.
